import 'package:flutter/cupertino.dart';
import 'package:flutter_09_game/base/controller/base_list_controller.dart';
import 'package:flutter_09_game/base/controller/base_list_more_controller.dart';
import 'package:flutter_09_game/config/app_routes.dart';
import 'package:flutter_09_game/config/history_route_ovserver.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/pages/promotion/models/model_rate_setting.dart';
import 'package:flutter_09_game/pages/promotion/models/model_team_member_item.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/loading/loading_utils.dart';
import 'package:flutter_09_game/widgets/dialog/common_normal_sheet.dart';
import 'package:get/get.dart';

class PromotionCommissionLogic extends BaseListMoreController {
  RxBool hasMembers = false.obs;

  RxString rebateRateRx = ''.obs;

  List<ModelRateSetting> rateSettingList = [];
  Rxn<ModelRateSetting> selectItemRx =  Rxn();
  @override
  void onInit() {
    super.onInit();

  }

  @override
  Future<List> loadData() async {
    AppUserManager().refreshUserInfo();
    AppUserManager().loadWalletData();
    HttpResponse response = await HttpManager().get(
      API.getUserTeamMember,
      params: {
        'page': pageNum,
        'size': pageSize,
        'symbol': AppUserManager().currentWalletRx.value.symbol,
      },
      fromJson: ModelTeamMemberItem.fromJson,
    );
    if (response.isSuccess) {
      hasMembers.value = response.data.isNotEmpty;
    }

    return response.data ?? [];
    // return await loadTeamMembers();
  }

  /// 转为平台余额
  void onTransferToBalance() {
    // LoadingUtils.showToast('转为平台余额功能待实现');
    // TODO: 实现转为平台余额功能

    Get.toNamed(AppRoutes.promotionTransferCommissionPage);
    // 可能需要调用API: API.PromotiontransferCommissionToBalance
  }

  /// 提现至钱包
  void onWithdrawToWallet() {
    // LoadingUtils.showToast('提现至钱包功能待实现');
    // TODO: 实现提现至钱包功能
    Get.toNamed(AppRoutes.promotionWithdrawPage);
    // 可能需要调用API: API.withdrawCommissionToWallet
  }

  Future getRebateRateSetting() async {
    HttpResponse response = await HttpManager().get(
      API.getRebateRateSetting,
      params: {'page': 1, 'size': 99},
      uiType: HttpUIType.loadingAndToast,
      fromJson: ModelRateSetting.fromJson,
    );
    if (response.isSuccess) {
      rateSettingList = response.data;
    }
    // getRebateRateSetting
  }

  postUserChangeRebateRate(
      ModelTeamMemberItem member, ModelRateSetting? selectItem) async {
    HttpResponse response =
        await HttpManager().post(API.postUserChangeRebateRate,
            params: {
              // rebateRateRx.value.split('.').first
              'rebate_rate_setting_id': selectItem?.id,
              'sub_user_id': member.id,
            },
            uiType: HttpUIType.loadingAndToast
            // fromJson: ModelTeamMemberItem.fromJson,
            );
    if (response.isSuccess) {
      routeHistoryObserver.removeName(CommonNormalSheet.name);
      LoadingUtils.showToast(LocaleKeys.setSuccess.tr);
      member.rebateRate = selectItem?.rate;
      update();
    }
  }
}
